package com.example.qx.qxa;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;

import com.example.qx.R;
import com.example.qx.lei.shudu;
import com.example.qx.lei.time;

import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.ThreadPoolExecutor;

public class qx extends Fragment implements BDLocationListener {
    private static  final int PERMISSION_REQUEST_CODE=1;
    private TextView textView;
    private Button butks,butjs;
    private  shudu shudu= com.example.qx.lei.shudu.instance();



    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup viewGroup, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.qx,viewGroup,false);
        textView= view.findViewById(R.id.ssd);
        butks=view.findViewById(R.id.kaishiqingxing);
        butjs=view.findViewById(R.id.jieshuqixing);
        LocationClient.setAgreePrivacy(true);
        final weizhi  weizhi;
        try {
            weizhi = com.example.qx.qxa.weizhi.getInstance(getActivity());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        final time time= com.example.qx.lei.time.getInstance();

        if((ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)||
                (ActivityCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED))
             {
            ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_CODE);

              }



        butks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    weizhi.startMointor();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

                time.qidong().scheduleAtFixedRate(new TimerTask() {
                    int z=0;
                    List<Float> c=shudu.getSj();
                    @Override
                    public void run() {
                        Float a=c.get(z);
                        textView.setText(String.valueOf(a));
                        ++z;
                        c=shudu.getSj();
                    }
                }, 0, 3000);

            } });



        butjs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                weizhi.stopMonitor();
                shudu.delete();
                time.quxiao();
            }
                                                           });


        return view;
    }


    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        StringBuffer sf=new StringBuffer(256);
     if(null!=bdLocation&&bdLocation.getLocType()!=BDLocation.TypeServerError) {
        float a=1;
        a=jisuan(bdLocation.getLatitude(),bdLocation.getLongitude(),bdLocation)/2;
         sf.append(bdLocation.getLocType());
         System.out.println(a);
         System.out.println(sf);
         a=((float) Math.round(a*100))/100;
         shudu.add(a);
     }


    }

public float jisuan(double a,double b,BDLocation bdLocation){
        double c=0,d=0;
        double[] z;
        z=shudu.getjw();
        c=z[0];
        d=z[1];
        float[] e=new float[1];
        e[0]=0;
    System.out.println(a);
    System.out.println(b);
            shudu.setjw(a,b);

        if(c!=0||d!=0) {
            Location.distanceBetween(a,b,c,d,e);
        }




    return  e[0];
}



}
